﻿using System;
using System.Diagnostics;
using System.IO;
using System.Text;

namespace StockLib.UserData
{
    public class TabFileSaver
    {
        public virtual void Save()
        {
            FileInfo fi = new FileInfo(StockUserData.Instance.GetFileName(UserDataType.Tab));
            FileStream fs = null;
            StreamWriter sw = null;

            try
            {
                if (!fi.Directory.Exists)
                    Directory.CreateDirectory(fi.Directory.FullName);

                fs = new FileStream(fi.FullName, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
                sw = new StreamWriter(fs, Encoding.Default);

                foreach (StockTab domain in StockTabContainer.Instance)
                {
                    //string line = string.Format("{0},{1}", domain.Name, domain.BlockId);
                    //sw.WriteLine(line);
                    sw.WriteLine(domain.BlockId);
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.ToString());
                throw ex;
            }
            finally
            {
                if (sw != null)
                {
                    sw.Close();
                    sw.Dispose();
                }
                if (fs != null)
                {
                    fs.Close();
                    fs.Dispose();
                }
            }
        }
    }
}
